﻿
namespace DafYomiPortalLib
{
    using System;
    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    using System.Globalization;
    using System.IO;
    using System.Net;
    using System.Runtime.Serialization;
    using System.Runtime.Serialization.Json;
    using System.Threading.Tasks;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;
    using System.Windows.Ink;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Shapes;
    using DafYomiLib;

    public class MediaPaths
    {
        private const string s_portalMediaBaseUrl = @"http://files.daf-yomi.com/";

        private const string s_lessonsMetaDataUrl = @"http://files.daf-yomi.com/app/magid_shiors.xml";

        private const string s_mediaLinkJsonUrlFormat =
            @"http://daf-yomi.com/mobile/jsonservice.ashx?medialink=1&maggidshiur={0}&massechet={1}&daf={2}";

        private const int c_tractatesBaseIndex = 282;

        static MediaPaths()
        {
            TractateToMediaPath = new Dictionary<Tractate, string>
                {
                    { Tractate.Brachot, "brachot" },
                    { Tractate.Shabat, "shabat" },
                    { Tractate.Eiruvin, "eruvin" },
                    { Tractate.Psachim, "psachim" },
                    { Tractate.Shkalim, "shkalim" },
                    { Tractate.Yoma, "yoma" },
                };
        }

        public static IDictionary<Tractate, string> TractateToMediaPath { get; private set; }

        public static string LessonsMetadataUrl
        {
            get { return s_lessonsMetaDataUrl; }
        }

        public static string MediaRoot 
        {
            get { return s_portalMediaBaseUrl; }
        }

        public static async Task<string> GetMediaUrl(Rabbi rabbi, Tractate tractate, int pageNumber, bool audio = true)
        {
            string mediaLinkJsonUrl = string.Format(CultureInfo.InvariantCulture,
                                                    s_mediaLinkJsonUrlFormat,
                                                    rabbi.Id,
                                                    tractate + c_tractatesBaseIndex,
                                                    pageNumber);

            var request = (HttpWebRequest)WebRequest.Create(mediaLinkJsonUrl);

            var response = (HttpWebResponse)await request.GetResponseAsync();
            if (response == null)
            {
                return null;
            }

            var jsonSerializer = new DataContractJsonSerializer(typeof(MediaLink));
            var mediaLink = jsonSerializer.ReadObject(response.GetResponseStream()) as MediaLink;
            if (mediaLink == null)
            {
                return null;
            }

            return mediaLink.Url;
        }

        [DataContract]
        private class MediaLink 
        {
            [DataMember(Name = "link")]
            public string Url { get; set; }
        }
    }
}
